﻿using Myotragus.Data.Domain;
using Myotragus.Data.Emit;
using Myotragus.Data.Metadata;
using NHibernate.Criterion;

namespace Myotragus.Data.Criteria
{
	public class DefaultsLocalizableEntityProjector<TEntity> : LocalizableEntityProjector<TEntity>,
																 IDefaultsLocalizableEntityProjector<TEntity>
		where TEntity : IEntity
	{
		public DefaultsLocalizableEntityProjector(IDefaultsLocalizableEntityDescriptor<TEntity> descriptor,
			ILocalizableEntityMapping mapping)
			: base(descriptor, mapping)
		{
		}

		protected override IProjection CreateLocalizablePropertyProjection(string property, string path)
		{
			return Projections.GroupProperty(
				Projections.Conditional(
					Property.ForName(string.Format("{0}.Value", property)).IsNotNull(),
					Projections.Property(string.Format("{0}.Value", property)),
					Projections.Property(string.Concat(
						EmitHelper.CreateAlias("DefaultValues", property), ".Value"))
				));
		}
	}
}